package com.quo.controller;

import java.util.ArrayList;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.quo.entity.Customer;
import com.quo.entity.Customer2;
import com.quo.entity.CustomerUpdate;

import com.quo.entity.Dept;
import com.quo.entity.Emp;
import com.quo.entity.EmpLogin;
import com.quo.entity.EmpRole;
import com.quo.entity.ProductSeries;
import com.quo.entity.ProductSeries2;
import com.quo.entity.Productdel;
import com.quo.entity.Updatedel;
import com.quo.exceptions.LoginException;
import com.quo.service.CustomerService;
import com.quo.service.DeptService;
import com.quo.service.EmpService;
import com.quo.service.UserService;
import com.quo.utils.Result;
import com.quo.utils.ResultCode;

/**
 * 
 * @author 韩宛廷
 *
 */
@RestController("customerController")
@RequestMapping(value = "/api")
public class CustomerController {
	@Autowired
	public CustomerService cService;
	@Autowired
	public EmpService eService;

	/**
	 * 获取所有的顾客信息
	 * 
	 */
	@RequestMapping(value = "/customer-all", method = RequestMethod.GET)
	public Result findCustomerAll() {

		List<Customer2> clist = cService.getAll();
		Result result = new Result(ResultCode.SUCCESS);
		System.out.println(clist);
		result.setData(clist);
		return result;

//		Result result = new Result();
//		int eno;
//		try {
//			eno = Integer.parseInt(enostr);
//		} catch (NumberFormatException n) {
//			result = new Result(ResultCode.FAIL);
//			result.setMessage("IDまたはパスワードが正しくありません。");
//			return result;
//		}
//		EmpRole emp = cService.getRidByEno(eno);
//		int rid = emp.getRid();
//		if (rid == 1001) {
//			List<Customer2> clist = cService.getAll();
//			result = new Result(ResultCode.SUCCESS);
//			System.out.println(clist);
//			result.setData(clist);
//			return result;
//		} else if (rid == 1002) {
//			List<Customer2> clist = cService.getAll();
//			result = new Result(ResultCode.SUCCESS);
//			System.out.println(clist);
//			result.setData(clist);
//			return result;
//		} else if (rid == 2001) {
//			List<Customer2> clist = cService.getAll();
//			result = new Result(ResultCode.SUCCESS);
//			System.out.println(clist);
//			result.setData(clist);
//			return result;
//		} else if (rid == 1003) {
//			List<Customer2> clist2 = cService.getAllByEno(eno);
//			result = new Result(ResultCode.SUCCESS);
//			result.setData(clist2);
//			return result;
//		} else if (rid == 3001) {
//			List<Customer2> clist2 = cService.getAllByEno(eno);
//			result = new Result(ResultCode.SUCCESS);
//			result.setData(clist2);
//			return result;
//		}
//		return result;
//
	}

	/**
	 * 获取自己所有的顾客信息
	 * 
	 * @param cid
	 * 
	 * @return
	 */
	@RequestMapping(value = "/customer-all-personal", method = RequestMethod.GET)
	public Result getAllByEno(@CookieValue(name = "a", required = false) String enostr) {

		Result result = new Result();
		int eno;
		try {
			eno = Integer.parseInt(enostr);
		} catch (NumberFormatException n) {
			result = new Result(ResultCode.FAIL);
			result.setMessage("IDまたはパスワードが正しくありません。");
			return result;
		}
		List<Customer2> clist = cService.getAllByEno(eno);
		result = new Result(ResultCode.SUCCESS);
		result.setData(clist);
		return result;
//		EmpRole emp = cService.getRidByEno(eno);
//		int rid = emp.getRid();
//
//		if (rid == 1001 || rid == 1002) {
//			Customer customer = cService.getByCid(cid);
//			result = new Result(ResultCode.SUCCESS);
//			System.out.println(customer);
//			result.setData(customer);
//			return result;
//		} else {
//			Customer customer = cService.getCustomerByEno(eno, cid);
//			if (customer != null) {
//				result = new Result(ResultCode.SUCCESS);
//				result.setData(customer);
//				return result;
//			} else {
//				return new Result(ResultCode.UNAUTHORISE);
//			}
//		}

	}

	/**
	 * 获取所有顾客的单个详细信息
	 * 
	 * @param cid
	 * @return
	 */
	@RequestMapping(value = "/customer-all/{cid}", method = RequestMethod.GET)
	public Result findByCid(@PathVariable(value = "cid") int cid) {

		Customer customer = cService.getAllByCid(cid);
		Result result = new Result(ResultCode.SUCCESS);
		result.setData(customer);
		return result;

	}

	/**
	 * 获取自己所有的顾客信息
	 * 
	 * @param cid
	 * 
	 * @return
	 */
	@RequestMapping(value = "/customer-personal", method = RequestMethod.GET)
	public Result getPersonalAllByEno(@CookieValue(name = "a", required = false) String enostr) {

		Result result = new Result();
		int eno;
		try {
			eno = Integer.parseInt(enostr);
		} catch (NumberFormatException n) {
			result = new Result(ResultCode.FAIL);
			result.setMessage("IDまたはパスワードが正しくありません。");
			return result;
		}
		List<Customer2> clist = cService.getAllByEno(eno);
		result = new Result(ResultCode.SUCCESS);
		result.setData(clist);
		return result;
	}

	// 获取自己的单个顾客信息
	@RequestMapping(value = "/customer-personal/{cid}", method = RequestMethod.GET)
	public Result getCustomerByEno(@PathVariable(value = "cid") int cid,
			@CookieValue(name = "a", required = false) String enostr) {
		Result result = new Result();
		int eno;
		try {
			eno = Integer.parseInt(enostr);
		} catch (NumberFormatException n) {
			result = new Result(ResultCode.FAIL);
			result.setMessage("IDまたはパスワードが正しくありません。");
			return result;
		}
		Customer customer = cService.getCustomerByEno(eno, cid);
		if (customer != null) {
			result = new Result(ResultCode.SUCCESS);
			result.setData(customer);
			return result;
		} else {
			return new Result(ResultCode.UNAUTHORISE);
		}

	}

	// 更新顾客信息（可以更新所有人的信息）

	@RequestMapping(value = "/customer-all/{cid}", method = RequestMethod.PUT)
	public Result updateCustomerByCid(@PathVariable(value = "cid") int cid, @RequestBody CustomerUpdate customer) {
		Result result = new Result();
		cService.updateCustomerByCid(customer, cid);
		result = new Result(ResultCode.SUCCESS);
		return result;
	}
//		EmpRole emp = cService.getRidByEno(eno);
//		int rid = emp.getRid();
//
//		if (rid == 1001 || rid == 1002) {
//			cService.updateCustomerByCid(customer, cid);
//			result = new Result(ResultCode.SUCCESS);
//			return result;
//		} else {
//			boolean b = cService.updateCustomerByEno(customer, eno, cid);
//			if (b) {
//				result = new Result(ResultCode.SUCCESS);
//				return result;
//			} else {
//				result = new Result(ResultCode.UNAUTHORISE);
//				return result;
//			}
//		}
//	}

	// 更新顾客信息（只能更新他自己创建的顾客信息）

	@RequestMapping(value = "/customer-personal/{cid}", method = RequestMethod.PUT)
	public Result updateCustomerByCidAndEno(@RequestBody CustomerUpdate customer,
			@CookieValue(name = "a", required = false) String enostr, @PathVariable(value = "cid") int cid) {
		Result result = new Result();
		int eno;
		try {
			eno = Integer.parseInt(enostr);
		} catch (NumberFormatException n) {
			result = new Result(ResultCode.FAIL);
			result.setMessage("IDまたはパスワードが正しくありません。");
			return result;
		}
		boolean b = cService.updateCustomerByEno(customer, eno, cid);
		if (b) {
			result = new Result(ResultCode.SUCCESS);
			return result;
		} else {
			result = new Result(ResultCode.UNAUTHORISE);
			return result;
		}
	}

	@RequestMapping(value = "/customer-able", method = RequestMethod.GET)
	public Result updateAble(@CookieValue(name = "a", required = false) String enostr) {
		Result result = new Result();
		int eno;
		try {
			eno = Integer.parseInt(enostr);
		} catch (NumberFormatException n) {
			result = new Result(ResultCode.FAIL);
			result.setMessage("IDが正しくありません。");
			return result;
		}

		List<Integer> data = new ArrayList<>();
		List<Customer2> customers = cService.getAllByEno(eno);
		for (Customer2 customer : customers) {

			// Updatedel updel=new Updatedel(customer2.getCid(), false);
			data.add(customer.getCid());
		}

//		for (ProductSeries pro : customer2) {
//			Productdel prodel = new Productdel(pro.getPsid(), psService.getDelEnableByPsid(pro.getPsid()));
//			data.add(prodel);
//		}

		result = new Result(ResultCode.SUCCESS);
		result.setData(data);
		return result;
	}

	@RequestMapping(value = "/customer", method = RequestMethod.POST)
	public Result save(@CookieValue(name = "a", required = false) String enostr, @RequestBody CustomerUpdate customer) {
		Result result = new Result();
		int eno;
		try {
			eno = Integer.parseInt(enostr);
		} catch (NumberFormatException n) {
			result = new Result(ResultCode.FAIL);
			result.setMessage("IDまたはパスワードが正しくありません。");
			return result;
		}
		cService.save(customer, eno);
		return new Result(ResultCode.SUCCESS);

	}
}
